package com.ants.visualizer;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.niftygui.NiftyJmeDisplay;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import com.jme3.terrain.geomipmap.TerrainLodControl;
import com.jme3.terrain.geomipmap.TerrainQuad;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture.WrapMode;
import com.jme3.material.Material;
import com.ants.visualizer.AntsHeightMap;
import com.jme3.bounding.BoundingBox;
import com.jme3.light.AmbientLight;
import com.jme3.renderer.Camera;
import com.jme3.terrain.heightmap.AbstractHeightMap;
import java.util.ArrayList;
import java.util.List;
/**
 * test
 * @author normenhansen
 */
public class Main extends SimpleApplication implements ScreenController {

    private Nifty nifty;
    private TerrainQuad terrain;
    private AbstractHeightMap heightmap;
    public static void main(String[] args) {
        Main app = new Main();
        app.start();
    }

    public void initMenu() {
        NiftyJmeDisplay niftyDisplay = new NiftyJmeDisplay(assetManager,
                inputManager,
                audioRenderer,
                guiViewPort);
        nifty = niftyDisplay.getNifty();
        nifty.fromXml("Interface/MainMenu.xml","start",this);
        guiViewPort.addProcessor(niftyDisplay);
        flyCam.setDragToRotate(true);
    }

    @Override
    public void simpleInitApp() {
        initMenu();
    }        

    @Override
    public void simpleUpdate(float tpf) {
        //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
        //TODO: add render code
    }

    @Override
    public void bind(Nifty nifty, Screen screen) {
        System.out.println("bind( " + screen.getScreenId() + ")");
    }

    @Override
    public void onStartScreen() {
        System.out.println("onStartScreen");
    }

    @Override
    public void onEndScreen() {
        System.out.println("onEndScreen");
    }

    public void quit(){
        nifty.gotoScreen("end");
        stop();
    }
    
    public void loadReplay() {
        nifty.gotoScreen("end");
        heightmap = null;
        heightmap = new AntsHeightMap();
        heightmap.load();
        createTerrain();   
        flyCam.setDragToRotate(false);
    }
    
    private void createTerrain() {
        terrain = new TerrainQuad("terrain",33,129,heightmap.getHeightMap());
        Material terrain_mat = new Material(assetManager,"Common/MatDefs/Terrain/Terrain.j3md");

        Texture dirt = assetManager.loadTexture("Textures/GroundTextures/dirt.jpg");
        dirt.setWrap(WrapMode.Repeat);
        terrain_mat.setTexture("Tex1",dirt);
        terrain_mat.setFloat("Tex1Scale",32f);
        
//        Texture grass = assetManager.loadTexture("Textures/GroundTextures/GRS_sq_020_Render_large_surface.jpg");
//        grass.setWrap(WrapMode.Repeat);
//        terrain_mat.setTexture("Tex2",grass);
//        terrain_mat.setFloat("Tex2Scale",32f);
        
        
        terrain.setMaterial(terrain_mat);
        terrain.setModelBound(new BoundingBox());
        terrain.updateModelBound();
        terrain.setLocalScale(2f,1f,2f);
        List<Camera>  cameras = new ArrayList<Camera>();
        cameras.add(getCamera());
        TerrainLodControl control = new TerrainLodControl(terrain,cameras);
        terrain.addControl(control);
        rootNode.attachChild(terrain);
    }

}
